﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Diagnostics;
using MeizuSDK.Drawing;

namespace MeizuSDK.Presentation
{
    /// <summary>
    /// 工具条控件（对应UiToolBarPro）
    /// </summary>
    public class ToolBar : Control
    {
        #region DllImport
        [DllImport("MZFCWrapper.dll")]
        private static extern IntPtr UiToolBarProCtor();

        [DllImport("MZFCWrapper.dll")]
        private static extern uint UiToolBarProGetDisableTextColor(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiToolBarProSetDisableTextColor(IntPtr pObj, uint clrText);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiToolBarProEnableButton(IntPtr pObj, ToolBarButtonIndex index, bool bEnable);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiToolBarProEnableSeparateLine(IntPtr pObj, int index, bool bEnable);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiToolBarProSetButton(IntPtr pObj, ToolBarButtonIndex nIndex, [MarshalAsAttribute(UnmanagedType.I1)] bool bShow, [MarshalAsAttribute(UnmanagedType.I1)] bool bEnable, [InAttribute()] [MarshalAsAttribute(UnmanagedType.LPWStr)] string strText);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiToolBarProSetMiddleButton(IntPtr pObj, bool bShow, bool bEnable, [InAttribute()] [MarshalAsAttribute(UnmanagedType.LPWStr)] string strText, IntPtr img, IntPtr imgHL, IntPtr disImg);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiToolBarProSetIconButton(IntPtr pObj, ToolBarButtonIndex index, bool bShow, bool bEnable, IntPtr img, IntPtr imgHL, IntPtr disImg);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiToolBarProSetIconButtonImage(IntPtr pObj, ToolBarButtonIndex index, IntPtr img, IntPtr imgHL, IntPtr disImg);
        #endregion

        #region 事件
        /// <summary>
        /// 当用户点击工具条上按钮时引发此事件
        /// </summary>
        public event EventHandler<ButtonEventArgs> ButtonClick;
        #endregion

        #region 常量
        /// <summary>
        /// 获取工具栏高度
        /// </summary>
        public const int HEIGHT = MzMetrics.MZM_HEIGHT_TOOLBARPRO;
        #endregion

        #region 公共属性
        /// <summary>
        /// 获取或设置工具条按钮被禁用后的文本颜色
        /// </summary>
        public Color DisabledTextColor
        {
            get
            {
                uint clr = UiToolBarProGetDisableTextColor(Handle);

                return MzDrawHelper.ConvertRgbToColor((int)clr);
            }
            set
            {
                uint clr = (uint)MzDrawHelper.GetRGB(value);

                UiToolBarProSetDisableTextColor(Handle, clr);
            }
        }
        #endregion

        #region 构造函数
        /// <summary>
        /// 构造工具条对象
        /// </summary>
        public ToolBar()
        {
            Handle = UiToolBarProCtor();
            SetID();

            Size = new Size(Screen.ClientWidth, HEIGHT);
            ResetLocation();

            Application.TaskBarVisibleChanged += new EventHandler(Application_TaskBarVisibleChanged);
        }
        #endregion

        #region 函数
        /// <summary>
        /// 当系统任务栏可视性发生变化时触发此事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Application_TaskBarVisibleChanged(object sender, EventArgs e)
        {
            ResetLocation();
        }
        /// <summary>
        /// 重设工具条位置
        /// </summary>
        private void ResetLocation()
        {
            Location = new Point(0, Screen.ClientHeight - HEIGHT);// 调整工具条位置
        }
        /// <summary>
        /// 启用或禁用指定索引的按钮
        /// </summary>
        /// <param name="index">按钮索引</param>
        /// <param name="enabled">是否启用按钮</param>
        public void EnableButton(ToolBarButtonIndex index,bool enabled)
        {
            UiToolBarProEnableButton(Handle, index, enabled);
        }
        /// <summary>
        /// 启用或禁用分隔条
        /// </summary>
        /// <param name="index">分隔条的索引</param>
        /// <param name="enabled">是否启用此分隔条</param>
        public void EnableSeparateLine(int index,bool enabled)
        {
            UiToolBarProEnableSeparateLine(Handle, index, enabled);
        }
        /// <summary>
        /// 设置工具栏文本按钮
        /// </summary>
        /// <param name="index">工具按钮的位置索引</param>
        /// <param name="isShow">是否显示按钮</param>
        /// <param name="isEnabled">是否启用按钮</param>
        /// <param name="text">按钮文本</param>
        public void SetTextButton(ToolBarButtonIndex index, bool isShow, bool isEnabled, string text)
        {
            switch (index)
            {
                case ToolBarButtonIndex.LeftTextButton:
                case ToolBarButtonIndex.RightTextButton:
                    UiToolBarProSetButton(Handle, index, isShow, isEnabled, text);
                    break;
                case ToolBarButtonIndex.MiddleTextButton:
                    SetMiddleButton(isShow, isEnabled, text, null, null, null);
                    break;
            }
        }
        /// <summary>
        /// 设置工具栏中间按钮（文本按钮，如果设置了图标，则是图标+文本。返回的ToolBarButtonIndex=MiddleTextButton）
        /// </summary>
        /// <param name="index">工具按钮的位置索引</param>
        /// <param name="isShow">是否显示按钮</param>
        /// <param name="isEnabled">是否启用按钮</param>
        /// <param name="text">按钮文本</param>
        /// <param name="img">按钮图标</param>
        /// <param name="imgHighlight">按钮高亮图标（按钮按下时的图标）</param>
        /// <param name="imgDisabled">按钮被禁用时的图标</param>
        public void SetMiddleButton(bool isShow, bool isEnabled, string text,
            ImagingHelper img, ImagingHelper imgHighlight, ImagingHelper imgDisabled)
        {
            UiToolBarProSetMiddleButton(Handle, isShow, isEnabled, text,
                (img == null ? IntPtr.Zero : img.Handle),
                (imgHighlight == null ? IntPtr.Zero : imgHighlight.Handle),
                (imgDisabled == null ? IntPtr.Zero : imgDisabled.Handle));
        }
        /// <summary>
        /// 设置工具栏中间图标按钮（图标按钮可以设置三个）
        /// </summary>
        /// <param name="index">工具按钮的位置索引</param>
        /// <param name="isShow">是否显示按钮</param>
        /// <param name="isEnabled">是否启用按钮</param>
        /// <param name="text">按钮文本</param>
        /// <param name="img">按钮图标</param>
        /// <param name="imgHighlight">按钮高亮图标（按钮按下时的图标）</param>
        /// <param name="imgDisabled">按钮被禁用时的图标</param>
        public void SetIconButton(ToolBarButtonIndex index, bool isShow, bool isEnabled,
            ImagingHelper img, ImagingHelper imgHighlight, ImagingHelper imgDisabled)
        {
            switch (index)
            {
                case ToolBarButtonIndex.MiddleIconButton1:
                case ToolBarButtonIndex.MiddleIconButton2:
                case ToolBarButtonIndex.MiddleIconButton3:
                    UiToolBarProSetIconButton(Handle, index, isShow, isEnabled,
                        (img == null ? IntPtr.Zero : img.Handle),
                        (imgHighlight == null ? IntPtr.Zero : imgHighlight.Handle),
                        (imgDisabled == null ? IntPtr.Zero : imgDisabled.Handle));
                    break;
            }
        }
        /// <summary>
        /// 处理MzFC MzCommand消息
        /// </summary>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        internal override void OnMzCommand(uint wParam, uint lParam)
        {
            if (ButtonClick != null)
                ButtonClick(this, new ButtonEventArgs((ToolBarButtonIndex)lParam));
        }
        #endregion

        /// <summary>
        /// 工具栏按钮事件参数
        /// </summary>
        public class ButtonEventArgs : EventArgs
        {
            /// <summary>
            /// 构建工具条按钮事件参数
            /// </summary>
            /// <param name="index"></param>
            public ButtonEventArgs(ToolBarButtonIndex index)
            {
                Index = index;
            }
            /// <summary>
            /// 获取引发此事件的工具条按钮位置索引
            /// </summary>
            public ToolBarButtonIndex Index
            {
                get;
                private set;
            }
        }
    }
}
